package com.wxmblog.photo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxmblog.base.common.utils.TokenUtils;
import com.wxmblog.photo.common.rest.request.user.UserEditRequest;
import com.wxmblog.photo.common.rest.response.user.UserInfoResponse;
import com.wxmblog.photo.dao.FrUserDao;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxmblog.photo.entity.FrUserEntity;
import com.wxmblog.photo.service.FrUserService;
import org.springframework.transaction.annotation.Transactional;


@Service("frUserService")
public class FrUserServiceImpl extends ServiceImpl<FrUserDao, FrUserEntity> implements FrUserService {

    @Override
    public Long countByPhone(String phone) {
        Wrapper<FrUserEntity> frUserEntityWrapper = new QueryWrapper<FrUserEntity>().lambda().eq(FrUserEntity::getPhone, phone);
        return count(frUserEntityWrapper);
    }

    @Override
    public FrUserEntity getFrUserByPhone(String phone) {
        Wrapper<FrUserEntity> frUserEntityWrapper = new QueryWrapper<FrUserEntity>().lambda().eq(FrUserEntity::getPhone, phone);
        return getOne(frUserEntityWrapper);
    }

    @Transactional
    @Override
    public void editUser(UserEditRequest request) {

        FrUserEntity frUserEntity = new FrUserEntity();
        BeanUtils.copyProperties(request, frUserEntity);
        Wrapper<FrUserEntity> frUserEntityWrapper = new QueryWrapper<FrUserEntity>().lambda().eq(FrUserEntity::getId, TokenUtils.getOwnerId());
        this.baseMapper.update(frUserEntity, frUserEntityWrapper);
    }

    @Override
    public UserInfoResponse userInfo() {

        FrUserEntity frUserEntity = this.baseMapper.selectById(TokenUtils.getOwnerId());
        UserInfoResponse response = new UserInfoResponse();
        if (frUserEntity != null) {
            BeanUtils.copyProperties(frUserEntity, response);
        }
        return response;
    }
}
